package com.lyf.viewapp.gps;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.lyf.viewapp.util.LogUtils;
import com.lyf.viewapp.util.Util;

/**
 * Created by lyf on 2018/6/25 10:16
 *
 * @author lyf
 * desc：定位服务
 */
public class LocationService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LocationBinder binder = new LocationBinder();
        LogUtils.showToast(this, "LocationService--->onBind()");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LocationTool.registerLocation(this, 2000, 5, new LocationTool.OnLocationChangeListener() {
            @Override
            public void getLastKnownLocation(Location location) {

            }

            @Override
            public void onLocationChanged(Location location) {
                LogUtils.showToast(LocationService.this, "x:" + location.getLatitude()
                        + "\ny:"+ location.getLongitude());
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }
        });
    }

    @Override
    public void onDestroy() {
        LocationTool.unRegisterLocation();
        LogUtils.showToast(this, "LocationService--->onDestroy()");
        super.onDestroy();
    }


    /**
     * IBinder是远程对象的基本接口，是为高性能而设计的轻量级远程调用机制的核心部分。但它不仅用于远程
     * 调用，也用于进程内调用。这个接口定义了与远程对象交互的协议。
     * 不要直接实现这个接口，而应该从Binder派生。
     * Binder类已实现了IBinder接口
     */
    public class LocationBinder extends Binder {
        /**
         * 获取Service的方法
         */
        public LocationService getService() {
            return LocationService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(12346, Util.buildNotification());
        return Service.START_STICKY;
    }

    public void stopService() {
        stopSelf();
    }
}
